package services

import (
	"github.com/zw2582/ginlib"
	"go.uber.org/zap"
	"strings"
	"szxf/models"
	"fmt"
)

//Valid 校验参数
func (this *IdNameService) OcrValid(accountId int) models.ResultCode {
	//校验商家是否存在
	account := models.AccountGet(accountId)
	if account.Id == 0 {
		return models.RC_NO_ACCOUNT
	}
	//校验余额是否充足
	if account.MonthCheck == 0 && account.Balance < account.PriceId {
		return models.RC_NO_MONEY
	}
	//校验sign是否正确
	rawSign := fmt.Sprintf("%d%s%s%s", this.LogReq.AccountId, this.LogReq.Name, this.LogReq.IdCard, account.AppKey)
	serverSign := ginlib.Md5encode(rawSign)
	if strings.ToLower(this.LogReq.Sign) != strings.ToLower(serverSign) {
		ginlib.Logger.Info("fail sign", zap.String("serverSign", serverSign), zap.String("rawSign", rawSign))
		return models.RC_SIGN_FAIL
	}
	//校验身份证号码是否正确
	idcard := []byte(this.LogReq.IdCard)
	if !ginlib.IsValidCitizenNo(&idcard) {
		return models.RC_ID_ILLEGAL_CARD
	}
	return nil
}
